package cn.com.callback

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONArray
import org.json.JSONObject
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: ApiBPLogin* @Description:深圳宝安排水小程序登录接口--外部接口
 * @author: chieh* @date 2020-11-08 21:28:00
 *
 */
public class ApiBPUnlogin implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(ApiBPUnlogin.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start ApiBPLogin 开始-----------------");

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String reJson = null;
        long endTime = 0;

        //获取参数js_code，登录时获取的 code
        def open_id = request.getParameter("open_id");
        log.debug("open_id" + open_id);

        try {
            //必填项校验
            String dealMessage = KHUtil.isNullMSG("openid", open_id);
            if (!KHUtil.isNull(dealMessage)) {
                endTime = System.currentTimeMillis();
                log.warn(dealMessage);
                reJson = "{\"result\":\"0\",\"resultmessage\":\"处理失败(" + dealMessage + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;

            } else {
                //处理客户
                String deleteSQL = "update customer set wechatid = null where wechatid = ?";
                int deleteFlag = jt.update(deleteSQL, open_id);
                log.debug("查看客户是否存在sql结果:" + deleteFlag);
                reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"注销成功，调用处理时长(毫秒):" + (endTime - startTime) + "\"}";
                return reJson;

            }

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"获取的登录接口调用处理失败,请检查确认入参有效且合法\",\"data\":[]}";

        }
        return reJson;

    }
}